home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_square_ridges.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.0 KB  |  73 lines

  1. // K-3D
  2. // Copyright (c) 1995-2004, Timothy M. Shead
  3. //
  4. // Contact: tshead@k-3d.com
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public
  8. // License as published by the Free Software Foundation; either
  9. // version 2 of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. // General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public
  17. // License along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. /** \file
  21.         \author Tim Shead (tshead@k-3d.com)
  22. */
  23.  
  24. /// Filtering code courtesy of the Advanced RenderMan book ... where else?
  25.  
  26. #define MIN_FILTER_WIDTH 1.0e-6
  27. #define filter_width(x) max(abs(Du(x)*du) + abs(Dv(x)*dv), MIN_FILTER_WIDTH)
  28.  
  29. /*
  30. float pulse(float edge0, edge1, x)
  31. {
  32.     return step(edge0, x) - step(edge1, x);
  33. }
  34.  
  35. float pulse_train(float edge, period, x)
  36. {
  37.     return pulse(edge, period, mod(x, period));
  38. }
  39. */
  40.  
  41. float filtered_pulse_train(float edge, period, x, dx)
  42. {
  43.     float w = dx / period;
  44.     float x0 = x/period - w/2;
  45.     float x1 = x0 + w;
  46.     float nedge = edge / period;
  47.     
  48.     float integral(float t)
  49.     {
  50.         extern float nedge;
  51.         return ((1 - nedge) * floor(t) + max(0, t-floor(t)-nedge));
  52.     }
  53.     
  54.     return (integral(x1) - integral(x0)) / w;
  55. }
  56.  
  57. displacement k3d_square_ridges(
  58.     float Km = 1.0;
  59.     float Frequency = 8.0;
  60.     float Offset = 0.25;
  61.     )
  62. {
  63.     float ridge_position = filtered_pulse_train(0.5 / Frequency, 1.0 / Frequency, t + (Offset / Frequency), filter_width(t));
  64. //    float ridge_position = pulse_train(0.5 / Frequency, 1.0 / Frequency, u, filter_width(u));
  65.  
  66.     vector Nn = normalize(N);
  67.     P += Nn * ((Km * ridge_position) / length(vtransform("shader", Nn)));
  68. //    P += Km * ridge_position * N;
  69.     N = calculatenormal(P);
  70. }
  71.  
  72.  
  73.